package com.wc.算法基础课.E第五讲动态规划.树状DP.病毒溯源;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/31 16:53
 * @description https://www.acwing.com/problem/content/3468/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010;
    static int[][] t = new int[N][2];
    // 表示他们的i的父亲
    static boolean[] st = new boolean[N];
    // f[i]这个子树的最大长度
    static int[] f = new int[N];
    static int n, k, maxLen;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            k = sc.nextInt();
            t[i] = new int[k];
            for (int j = 0; j < k; j++) {
                t[i][j] = sc.nextInt();
                st[t[i][j]] = true;
            }
            // 保证第一个找到的是最小序的
            if (k >= 2) Arrays.sort(t[i]);
        }
        int root = 0;
        // 找到根节点
        while (st[root]) root++;
        dfs(root);
        out.println(f[root]);
        maxLen = f[root];
        dfs(root, new int[maxLen], 0);
        out.flush();
    }

    static boolean isPrint = false;

    static void dfs(int u, int[] path, int step) {
        path[step] = u;
        if (step == maxLen - 1) {
            isPrint = true;
            out.print(path[0]);
            for (int i = 1; i <= step; i++) {
                out.print(" " + path[i]);
            }
            return;
        }
        for (int i = 0; i < t[u].length; i++) {
            dfs(t[u][i], path, step + 1);
            if (isPrint) return;
        }
    }

    static void dfs(int u) {
        f[u] = 1;
        for (int i = 0; i < t[u].length; i++) {
            dfs(t[u][i]);
            f[u] = Math.max(f[u], f[t[u][i]] + 1);
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
